/**
 * Copyright (C) 2009 aileron.cc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cc.aileron.orm;

/**
 * 
 * 基本的な検索条件
 * 
 * @author Aileron
 * 
 */
public class G2DaoFindCondition
{
    /**
     * all
     * 
     * @author Aileron
     * 
     */
    public static class All
    {
        All()
        {
        }
    }

    /**
     * id
     * 
     * @author Aileron
     */
    public static class Id
    {
        /**
         * @param id
         */
        public Id(final int id)
        {
            this.id = id;
        }

        /**
         * @param id
         */
        public Id(final String id)
        {
            this.id = id;
        }

        /**
         * id
         */
        public final Object id;
    }

    /**
     * paging
     * 
     * @author Aileron
     * 
     */
    public static class Paging
    {
        /**
         * @return limit
         */
        public int limit()
        {
            if (isCurrent)
            {
                return size * (maxPage() + 1);
            }
            return size * (page + 1);
        }

        /**
         * @return maxPage
         */
        public int maxPage()
        {
            return max / size;
        }

        /**
         * @return offset
         */
        public int offset()
        {
            if (isCurrent)
            {
                return size * maxPage();
            }
            return size * page;
        }

        /**
         * default constractor
         */
        public Paging()
        {
        }

        /**
         * set constractor size
         * 
         * @param size
         */
        public Paging(final int size)
        {
            this.size = size;
        }

        /**
         * @param size
         * @param page
         */
        public Paging(final int size, final int page)
        {
            this.size = size;
            this.page = page;
        }

        /**
         * is-current
         */
        public boolean isCurrent;

        /**
         * max
         */
        public int max;

        /**
         * page
         */
        public int page;

        /**
         * size
         */
        public int size;
    }

    /**
     * all
     */
    public static final All all = new All();

    /**
     * @return findByCondition
     */
    public static All all()
    {
        return all;
    }

    /**
     * @param id
     * @return findByCondition
     */
    public static Id id(final int id)
    {
        return new Id(id);
    }

    /**
     * @param id
     * @return findByCondition
     */
    public static Id id(final String id)
    {
        return new Id(id);
    }

    /**
     * @param size
     * @return paging instance
     */
    public static Paging paging(final int size)
    {
        return new Paging(size);
    }

    /**
     * @param size
     * @param page
     * @return paging instance
     */
    public static Paging paging(final int size, final int page)
    {
        return new Paging(size, page);
    }
}
